package com.shizhanzhe.gxb.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.home.adapter.StoreAdapter;
import com.shizhanzhe.gxb.home.bean.Merchantlist;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.gxb.widget.LoadingDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * @author YJ
 * @date 2019/1/22
 */
public class StoreActivity extends SwipeBackActivity {
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.no_data)
    ImageView noData;
    private StoreAdapter mAdapter;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarView(R.id.bar_0_view)
                .init();
        initData();
    }

    @OnClick(R.id.backarea)
    public void onViewClicked() {
        finish();
    }

    private void initData() {
        topTitle.setText("高佣店铺");
        recycler.setLayoutManager(new GridLayoutManager(this,3));
        mAdapter = new StoreAdapter(StoreActivity.this);
        recycler.setAdapter(mAdapter);
        morestores();
        mAdapter.setListener(new StoreAdapter.StoreListener() {
            @Override
            public void click(Merchantlist.DataBean.MerchantListResponseBean.MallSearchInfoVoListBean item) {
                startActivity(new Intent(StoreActivity.this, MallGoodsActivity.class)
                        .putExtra("name", item.getMall_name()).putExtra("mall_id", item.getMall_id()));
            }
        });
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                morestores();
            }
        });
        srl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                morestores();
            }
        });
    }

    private void morestores() {
        LoadingDialog.showDialogForLoading(this, "数据加载中...", false);
        OkHttpDownloadJsonUtil.downloadJson(StoreActivity.this, Path.merchantlists(page,20), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                LoadingDialog.cancelDialogForLoading();
                srl.finishRefresh();
                srl.finishLoadMore();
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        final Merchantlist merchantlist = new Gson().fromJson(json, Merchantlist.class);
                        if (merchantlist.getData().getMerchant_list_response().getMall_search_info_vo_list().size() > 0) {
                            mAdapter.updateData(page,merchantlist.getData().getMerchant_list_response().getMall_search_info_vo_list());
                        } else {
                            if (page == 1) {
                                srl.setVisibility(View.GONE);
                                noData.setVisibility(View.VISIBLE);
                            } else {
                                Toast.makeText(StoreActivity.this, "不能再有更多了", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } catch (Exception e) {
                        Log.d("_____", e.toString());
                        Toast.makeText(StoreActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(StoreActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

}
